package org.budo.microsoft.api;

import java.util.Map;

import org.budo.microsoft.api.response.MicrosoftTokenResponse;
import org.budo.microsoft.api.response.TokenInfoResponse;
import org.budo.support.http.client.HttpUtil;
import org.budo.support.http.client.Response;
import org.budo.support.lang.util.MapUtil;

/**
 * @author lmw
 */
public class MicrosoftOauthApi extends AbstractMicrosoftApi {
    public MicrosoftTokenResponse token(String code) {
        String url = "https://login.microsoftonline.com/common/oauth2/v2.0/token" //
                + "?client_id=" + this.getAppKey() //
                + "&scope=https%3A%2F%2Fgraph.microsoft.com%2Fmail.read" //
                + "&code=" + code //
                + "&redirect_uri=" + this.getRedirectUri() //
                + "&grant_type=authorization_code" //
                + "&client_secret=" + this.getAppSecret();

        Response response = HttpUtil.post(url);
        return new MicrosoftTokenResponse(response);
    }

    public TokenInfoResponse getTokenInfo(String accessToken) {
        String url = "https://api.weibo.com/oauth2/get_token_info";
        Map<String, String> map = MapUtil.stringMap("access_token", accessToken);
        Response response = HttpUtil.post(url, map);
        return new TokenInfoResponse(response);
    }
}